//========================================================================
//Copyright 2007-2010 David Yu dyuproject@gmail.com
//------------------------------------------------------------------------
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at 
//http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//========================================================================

package com.dyuproject.kvstore;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 * Default serializers
 * 
 * @author David Yu
 * @created Jan 27, 2010
 */
public final class Serializers
{

    private Serializers() {}
    
    private static final Serializer<String> __utf8string = new Serializer<String>()
    {
        public String deser(byte[] nonNullValue)
        {
            try
            {
                return new String(nonNullValue, "UTF-8");
            }
            catch (UnsupportedEncodingException e)
            {
                throw new RuntimeException(e);
            }
        }

        public byte[] ser(String nonNullValue)
        {
            try
            {
                return nonNullValue.getBytes("UTF-8");
            }
            catch (UnsupportedEncodingException e)
            {
                throw new RuntimeException(e);
            }
        }
    };
    
    private static final Charset utf8 = Charset.forName("UTF-8");
    
    private static final Serializer<String> __utf8stringFromCharset = new Serializer<String>()
    {
        public String deser(byte[] nonNullValue)
        {
            return new String(nonNullValue, utf8);
        }

        public byte[] ser(String nonNullValue)
        {
            return nonNullValue.getBytes(utf8);
        }
    };

    private static final Serializer<String> __string = new Serializer<String>()
    {
        public String deser(byte[] nonNullValue)
        {
            return new String(nonNullValue);
        }

        public byte[] ser(String nonNullValue)
        {
            return nonNullValue.getBytes();
        }
    };
    
    /**
     * UTF-8 string serializer.  Binding deferred.
     */
    public static final Serializer<String> STRING = 
        "UTF-8".equals(Charset.defaultCharset().name()) ? __string : 
            (System.getProperty("charset.utf8")!=null ? __utf8stringFromCharset : __utf8string);
    
    /**
     * No op serializer.
     */
    public static final Serializer<byte[]> BYTE_ARRAY = new Serializer<byte[]>()
    {
        public byte[] deser(byte[] nonNullValue)
        {
            return nonNullValue;
        }

        public byte[] ser(byte[] nonNullValue)
        {
            return nonNullValue;
        }        
    };
    
    /**
     * Long serializer.
     */
    public static final Serializer<Long> LONG = new Serializer<Long>()
    {
        public Long deser(byte[] nonNullValue)
        {
            if (nonNullValue.length != 8)
            {
                throw new AssertionError("Invalid byte array length: " + nonNullValue.length);
            }
            long result = 0;
            for (int i = 7; i >= 0; i--)
            {
                result <<= 8;
                result = result ^ ((long)nonNullValue[i] & 0xFF);
            }
            return result;
        }

        public byte[] ser(Long nonNullValue)
        {
            final long asPrimitiveValue = nonNullValue.longValue();
            byte[] result = new byte[8];
            for (int i = 0; i < 8; i++)
            {
                result[i] = (byte)(asPrimitiveValue >>> (i * 8));
            }
            return result;
        }
    };

}
